﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MJM.CBD.Conn;
using MJM.SEDIC.EN;
using System.Data;
using System.Data.SqlClient;

namespace MJM.SEDIC.AD
{
    public static class PermisoUsuarioAD
    {
        public static List<PermisoUsuarioEN> Listar()
        {
            List<PermisoUsuarioEN> lst=new List<PermisoUsuarioEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_OPTUSU_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            
            try
            {
                cmd.Connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var obj = new PermisoUsuarioEN();
                    obj.ID = dr.GetInt32(dr.GetOrdinal("OPTUSU_P_inID"));
                    obj.IdUsuario = dr.GetInt32(dr.GetOrdinal("USUPAD_P_inID"));
                    obj.Estado = dr.GetInt32(dr.GetOrdinal("OPTUSU_inFLAELI"));
                    obj.FechaCreacion = dr.GetDateTime(dr.GetOrdinal("OPTUSU_dtFECCRE"));
                    obj.DNI = dr.GetString(dr.GetOrdinal("PAD_chNRODOC"));
                    lst.Add(obj);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();               
            }
            return lst;
        }

        public static bool EsContribuyente(string DNI)
        {
            SqlConnection conn=new SqlConnection(Cadena.GetCadenaSatmun());
            SqlCommand cmd = new SqlCommand("s_s_M_PERS_XNOM_XDOC_PASS", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            bool entro=false;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@iPERS_chDOCIDE", DNI));
                cmd.Parameters.Add(new SqlParameter("@iPERS_chNOMCOM", ""));
                cmd.Connection.Open();
                SqlDataReader dr=cmd.ExecuteReader();
                if (dr.Read())
                {
                    entro = true;
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return entro;
        }
    }
}
